package com.flyee.controller.timer{
	
	import com.flyee.controller.tools.cartoon.model.AppearanceConfig;
	import com.flyee.model.Charactor;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.viewer.CharactorViewer;
	import com.flyee.viewer.SkillViewer;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class BuffMagicTimer extends Timer{
		
		//施法时间, 毫秒
		public var enchantTime : int = 400;
		//特效时间
		public var buffTime : int = 15000;
		
		//释放技能者
		public var playerViewer : CharactorViewer;
		//接受技能者
		public var targetViewer : CharactorViewer;
		//技能效果
		public var skillViewer : SkillViewer;
		
		//效果处理链
		private var arr : Array = new Array();
		
		public function BuffMagicTimer(delay:Number, repeatCount:int=0){
			super(delay, repeatCount);
			this.addEventListener(TimerEvent.TIMER, timerHandler);
			arr.push(this.enchantHandler);
			arr.push(this.buffHandler);
		}
		
		private function timerHandler(event : TimerEvent) : void {
			if(arr.length == 0){
				this.finish();
				return ;
			}
			var handler : Function = arr[0] as Function;
			handler.call(null);
			
		}
		
		/**施法阶段*/
		private function enchantHandler() : void {
			if(currentCount==1){
				skillViewer.cartoon.gotoAndStop("READY");
				//设置魔法动画可见和位置
				skillViewer.visible = true;
			}
			if(currentCount*delay >= enchantTime){
				arr.shift();
				//人物施法状态设为false
				var player : Charactor = ObjectCollection.getCharacterByVid(playerViewer.vid);
				player.skillStatus = false;
//				if(playerViewer == ObjectCollection.masterViewer){
//					ObjectCollection.master.skillStatus = false;					
//				}
				//人物动画设为准备中
				var ct : CartoonTimer = ObjectCollection.cartoonTimerMap.get(playerViewer.vid) as CartoonTimer;
				ct.setCurrentStatus(AppearanceConfig.MOVE_BY_READY, ct.getDirect(), 0);
				skillViewer.cartoon.gotoAndStop("COMPL");
				
				//待完成，隐藏进度条， 
			}else{
				//推进进度条
			}
		}
		
		/**buff持续阶段*/
		private function buffHandler() : void {
			if(currentCount * delay >= enchantTime + buffTime){							
				arr.shift();	
			}
		}
		
		/**结束技能动画*/
		private function finish() : void {
			this.stop();
			this.removeEventListener(TimerEvent.TIMER, timerHandler);
			if(skillViewer && targetViewer.specialContainer.contains(skillViewer)){
				targetViewer.specialContainer.removeChild(skillViewer);
			}
			playerViewer = null;
			skillViewer.dispose();
			skillViewer = null;
		}
		
	}
}